package cn.com.wind.bdg.inmysql.inmysql.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.wind.bdg.inmysql.inmysql.entity.Info;
import cn.com.wind.bdg.inmysql.inmysql.utils.GetHTMLData;
import cn.com.wind.bdg.inmysql.inmysql.utils.GetHtml;
import cn.com.wind.bdg.inmysql.inmysql.utils.JsoupGetCode;

/**
 * 
 * @ClassName:  Data2ES   
 * @Description:将数据存入ES索引 
 * @author: gaoxiang
 * @date:   2020年8月20日 下午3:10:15
 */
@Service
public class Data2ES {
	private static Logger LOGGER = LoggerFactory.getLogger(Data2DataBase.class);
		@Autowired
		ESService esService;
		@Autowired
		GetHtml getHtml;
		public void method() throws Exception {
			List<String> resUrl = new ArrayList<String>();
			GetHTMLData getHTMLData = new GetHTMLData();
			resUrl = getHtml.method();
			JsoupGetCode jsoupGetCode = new JsoupGetCode();
			Info info = new Info();
			for(String url:resUrl) {
				info.setCOMPANY_NAME(getHTMLData.getCompanyName(url));
				info.setREGISTERED_ADDRESS(getHTMLData.getLocation(url));
				info.setPLANTING_VARIETIES(getHTMLData.getType(url));
				info.setPLANTING_DISTRICT(getHTMLData.getArea(url));
				info.setPLANTING_DISTRICT_CODE(jsoupGetCode.method(getHTMLData.getArea(url)));
				info.setREPORT_CODE(getHTMLData.getReportCode(url));
			try {
				esService.add("index_gap_21732", "index_gap_21732", info);
			} catch (Exception e) {
				//如果数据缺失，将相关信息存入error.log日志中
				LOGGER.error("{}数据缺失，无法存入es",getHTMLData.getCompanyName(url),e);
			}
		}
	}
}
